//
//  ZJFinishedBooksTableViewCell.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/3.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJFinishedBooksTableViewCell.h"

@interface ZJFinishedBooksTableViewCell ()
{
    UIButton *_addButton;/**< 加入选学 */
    UIButton *_deleButton;/**< 删除 */
}
/** 图片 */
@property (nonatomic, strong) UIImageView *imageview;
/** 题目 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 完成100% */
@property (nonatomic, strong) UILabel *finished;
/** 简介 */
@property (nonatomic, strong) UILabel *abstract;

/** 标注 */
@property (nonatomic, strong) UILabel *subLabel;
/** 剩余天数 */
@property (nonatomic, strong) UILabel *residueDay;
/** 分割线 */
@property (nonatomic, strong) UILabel *underline;
@end

@implementation ZJFinishedBooksTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.imageview];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.subLabel];
        [self.contentView addSubview:self.finished];
        [self.contentView addSubview:self.abstract];
        [self.contentView addSubview:self.residueDay];
        [self.contentView addSubview:self.underline];
        
        self.underline.sd_layout
        .bottomSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 0)
        .rightSpaceToView(self.contentView, 0)
        .heightIs(7.5 * [UIScreen zj_screenHScale]);
        
        self.imageview.sd_layout
        .topSpaceToView(self.contentView, 12 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .bottomSpaceToView(self.underline, 12 * [UIScreen zj_screenHScale])
        .widthIs(140 * [UIScreen zj_screenWScale]);
        
        self.subLabel.sd_layout
        .bottomEqualToView(self.imageview)
        .leftSpaceToView(self.imageview, 8.5 * [UIScreen zj_screenWScale])
        .widthIs(140 * [UIScreen zj_screenWScale])
        .heightIs([NSString heightWithTextFontSize:[UIFont subTitleTextFontSize]]);
        
        self.residueDay.sd_layout
        .bottomEqualToView(self.subLabel)
        .leftSpaceToView(self.subLabel, 0)
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .heightRatioToView(self.subLabel, 1);
        
        self.finished.sd_layout
        .topEqualToView(self.imageview)
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .widthIs(42 * [UIScreen zj_screenWScale])
        .heightIs([NSString heightWithTextFontSize:[UIFont subTitleTextFontSize]]);
        
        self.titleLabel.sd_layout
        .topEqualToView(self.imageview)
        .leftEqualToView(self.subLabel)
        .rightSpaceToView(self.finished, 0)
        .heightRatioToView(self.finished, 1);
        
        self.abstract.sd_layout
        .topSpaceToView(self.finished, 0 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.subLabel)
        .rightEqualToView(self.finished)
        .bottomSpaceToView(self.subLabel, 0 * [UIScreen zj_screenHScale]);
        
        //********* 设置侧滑模块
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 71 * [UIScreen zj_screenWScale], 125 * [UIScreen zj_screenHScale])];
        view.backgroundColor = self.contentView.backgroundColor;
        //侧滑下划线
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, view.height - 7.5 * [UIScreen zj_screenHScale], view.width, 7.5 * [UIScreen zj_screenHScale])];
        line.backgroundColor = self.underline.backgroundColor;
        [view addSubview: line];
        //删除
        _deleButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, view.width, (view.height - line.height - 2) / 2)];
        _deleButton.backgroundColor = [UIColor themeColor];
        [_deleButton setTitle:@"删除" forState:UIControlStateNormal];
        [_deleButton setImage:[UIImage imageNamed:@"learn_list_del"] forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _deleButton.titleLabel.font = [UIFont titleTextFont];
        [_deleButton addTarget:self action:@selector(action_deleButton:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:_deleButton];
        
        _deleButton.titleEdgeInsets = UIEdgeInsetsMake(25 * [UIScreen zj_screenHScale], -_deleButton.imageView.image.size.width, 0.0, 0.0);
        _deleButton.imageEdgeInsets = UIEdgeInsetsMake(- 20 * [UIScreen zj_screenHScale], (_deleButton.bounds.size.width - _deleButton.imageView.image.size.width)/2, 0.0,(_deleButton.bounds.size.width - _deleButton.imageView.image.size.width)/2);

        
        //加入选学
        _addButton = [[UIButton alloc]initWithFrame:CGRectMake(0, _deleButton.bottom + 2, view.width, _deleButton.height)];
        [_addButton setTitle:@"加入选学" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor textGrayColor] forState:UIControlStateNormal];
        [_addButton setImage:[UIImage imageNamed:@"learn_list_add"] forState:UIControlStateNormal];
        _addButton.titleLabel.font = _deleButton.titleLabel.font;
        _addButton.backgroundColor = [UIColor zj_colorWithHexString:@"ededed"];
        [_addButton addTarget:self action:@selector(action_addButton:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:_addButton];
        _addButton.titleEdgeInsets = UIEdgeInsetsMake(25 * [UIScreen zj_screenHScale], -_deleButton.imageView.image.size.width, 0.0, 0.0);
        _addButton.imageEdgeInsets = UIEdgeInsetsMake(- 20 * [UIScreen zj_screenHScale], (_addButton.bounds.size.width - _addButton.imageView.image.size.width)/2, 0.0,(_addButton.bounds.size.width - _addButton.imageView.image.size.width)/2);
        
        self.rightButtons = @[view];
        self.rightSwipeSettings.transition = MGSwipeStateSwippingLeftToRight;

    }
    return self;
}


#pragma mark - 点击事件
//删除
- (void)action_deleButton:(UIButton *)deleButton{
    [_addButton setTitle:@"已经加入选学" forState:UIControlStateNormal];
    
}
//加入选学
- (void)action_addButton:(UIButton *)addButton{
    [self addCollectionSuccess:^{//加入选学成功
        
    }];
}

//加入选学网络请求
- (void)addCollectionSuccess:(void(^)())success{
    NSDictionary *dict = @{@"USER_ID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,
                           @"CATEGORY":@"0",
                           @"COURSEWARE_ID":self.model.ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL addCollection] Parameter:dict Success:^(id object) {
        NetWorkModel* model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//加入成功
            [YJProgressHUD showMsgWithoutView:@"加入成功"];
            success();
        }else{
            [YJProgressHUD showMsgWithoutView:@"加入失败"];
        }
    } Fail:^(id object) {
         [YJProgressHUD showMsgWithoutView:@"加入失败"];
    }];
}


#pragma mark - Setter
- (void)setModel:(ZJStudySpacialDetailModel *)model{
    _model = model;
    [self.imageview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEIMAGEURL,model.IMAGEPHOTO_THUMBURL]] placeholderImage:PlaceholderImage];
    self.titleLabel.text = model.NAME;
    self.abstract.text = model.COMMENT;
    self.subLabel.text = model.CATEGORY_NAMES.firstObject[@"CATEGORY_NAME"];
    self.residueDay.text = @"";
    self.finished.text = @"100%";
}

#pragma mark - Getters


- (UIImageView *)imageview{
    if (_imageview == nil){
        _imageview = [UIImageView new];
        _imageview.contentMode = UIViewContentModeScaleAspectFit;
        _imageview.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
        _imageview.layer.borderWidth = 1 * [UIScreen zj_screenWScale];
    }
    return _imageview;
}


- (UILabel *)titleLabel{
    if (_titleLabel == nil){
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor textBlackColor];
        _titleLabel.font = [UIFont titleTextFont];
  
    }
    return _titleLabel;
}
- (UILabel *)finished{
    if (_finished == nil){
        _finished = [UILabel new];
        _finished.textColor = [UIColor themeColor];
        _finished.font = [UIFont titleTextFont];
    }
    return _finished;
}

- (UILabel *)abstract{
    if (_abstract == nil){
        _abstract = [UILabel new];
        _abstract.textColor = [UIColor textGrayColor];
        _abstract.font = [UIFont subTitleTextFont];
        _abstract.numberOfLines = 3;
    }
    return _abstract;
}

- (UILabel *)subLabel{
    if (_subLabel == nil){
        _subLabel = [UILabel new];
        _subLabel.textColor = [UIColor zj_colorWithHexString:@"a8a8a8"];
        _subLabel.font = [UIFont subTitleTextFont];
        
    }
    return _subLabel;
}
- (UILabel *)residueDay{
    if (_residueDay == nil){
        _residueDay = [UILabel new];
        _residueDay.textColor = [UIColor zj_colorWithHexString:@"a8a8a8"];
        _residueDay.font = [UIFont subTitleTextFont];
        _residueDay.textAlignment = NSTextAlignmentRight;
    }
    return _residueDay;
}

- (UILabel *)underline{
    if (_underline == nil){
        _underline = [UILabel new];
        _underline.backgroundColor = [UIColor viewBGGrayColor];
    }
    return _underline;
}








@end
